<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="期数" prop="qs">
        <el-date-picker clearable
                        v-model="queryParams.qs"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择期数">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="号码个数" prop="numsSize">
        <el-input
            v-model="queryParams.numsSize"
            placeholder="请输入号码个数"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开奖号码" prop="kjNum">
        <el-input
            v-model="queryParams.kjNum"
            placeholder="请输入开奖号码"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['caipiao:five:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['caipiao:five:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['caipiao:five:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['caipiao:five:export']"
        >导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="generateCode"
            v-hasPermi="['caipiao:one:export']"
        >生成号码
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="el-icon-download"
            size="mini"
            @click="checkCode"
            v-hasPermi="['caipiao:one:export']"
        >兑奖
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="jj"
            v-hasPermi="['caipiao:one:export']"
        >交集
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="bj"
            v-hasPermi="['caipiao:one:export']"
        >并集
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />
    <el-table v-loading="loading" :data="fiveList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="主键" align="center" prop="id"/>
      <el-table-column label="期数" align="center" prop="qs" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.qs, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- show-overflow-tooltip 超出部分不换行，用省略号-->
      <el-table-column label="号码" align="center" prop="nums" show-overflow-tooltip/>
      <el-table-column label="号码个数" align="center" prop="numsSize"/>
      <el-table-column label="开奖号码" align="center" prop="kjNum"/>
      <el-table-column label="是否中奖" align="center" prop="rightPl3"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['caipiao:five:edit']"
          >修改
          </el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['caipiao:five:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改方法5对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="期数" prop="qs">
          <el-date-picker clearable
                          v-model="form.qs"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择期数">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="号码" prop="nums">
          <el-input v-model="form.nums" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="号码个数" prop="numsSize">
          <el-input v-model="form.numsSize" placeholder="请输入号码个数"/>
        </el-form-item>
        <el-form-item label="开奖号码" prop="kjNum">
          <el-input v-model="form.kjNum" placeholder="请输入开奖号码"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="numsOpen" width="800px" append-to-body :close-on-click-modal="false">
      <el-input type="textarea" rows="25" v-model="nums"></el-input>
      <el-input v-model="nsize" readonly></el-input>
      <el-tag :type="tagType">{{ zj }}</el-tag>
      <el-button type="primary" @click="fj">反集</el-button>
    </el-dialog>
  </div>
</template>

<script>
import {listFive, getFive, delFive, addFive, updateFive, checkCode, generateCode} from "@/api/caipiao/five";
import Vue from "vue";
import {cal} from "@/api/caipiao/calpl5";

export default {
  name: "Five",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      selectedItem: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 方法5表格数据
      fiveList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      numsOpen: false,
      nums: '',
      nsize: 0,
      zj: "未中奖",
      tagType: "success",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        qs: Vue.prototype.formatDateYYYYMMDD(new Date()),
        nums: null,
        numsSize: null,
        kjNum: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询方法5列表 */
    getList() {
      this.loading = true;
      listFive(this.queryParams).then(response => {
        this.fiveList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        qs: null,
        nums: null,
        numsSize: null,
        kjNum: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      // this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.selectedItem = selection
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加方法5";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFive(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改方法5";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFive(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFive(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除方法5编号为"' + ids + '"的数据项？').then(function () {
        return delFive(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('caipiao/five/export', {
        ...this.queryParams
      }, `five_${new Date().getTime()}.xlsx`)
    },
    /** 生成号码 */
    generateCode() {
      if (!this.queryParams.qs) {
        this.$modal.alertError("请选择生成号码的期数")
        return
      }
      this.loading = true;
      generateCode(this.queryParams).then(response => {
        this.$modal.msgSuccess("生成号码成功");
        this.loading = false;
        this.getList()
      });
    }
    ,
    /** 生成号码 */
    checkCode() {
      if (!this.queryParams.qs) {
        this.$modal.alertError("请选择兑奖号码的期数")
        return
      }
      this.loading = true;
      checkCode(this.queryParams).then(response => {
        this.$modal.msgSuccess("兑奖成功");
        this.loading = false;
        this.getList()
      });
    }
    ,
    jj() {
      if (this.selectedItem.length < 2) {
        this.$modal.alertError("请选择号码，至少2注")
        return
      }
      let dataArr = []

      let lg = this.selectedItem.length
      for (let i = 0; i < lg; i++) {
        dataArr.push(this.selectedItem[i].nums);
      }
      cal(dataArr, 1).then(response => {
        this.loading = false;
        this.title = "号码"
        this.numsOpen = true
        this.nums = response.msg;
        this.nsize = response.size;
        if (this.nums.includes(this.selectedItem[0].kjNum.substring(0, 3))) {
          this.zj = "中奖"
          this.tagType = "danger"
        } else {
          this.zj = "未中奖"
          this.tagType = "success"
        }
      });
    }
    ,
    bj() {
      if (this.selectedItem.length < 2) {
        this.$modal.alertError("请选择号码，至少2注")
        return
      }
      let dataArr = []

      let lg = this.selectedItem.length
      for (let i = 0; i < lg; i++) {
        dataArr.push(this.selectedItem[i].nums);
      }
      cal(dataArr, 2).then(response => {
        this.loading = false;
        this.title = "号码"
        this.numsOpen = true
        this.nums = response.msg;
        this.nsize = response.size;
        if (this.nums.includes(this.selectedItem[0].kjNum.substring(0, 3))) {
          this.zj = "中奖"
          this.tagType = "danger"
        } else {
          this.zj = "未中奖"
          this.tagType = "success"
        }
      });
    }
    ,
    fj() {
      let dataArr = []
      dataArr.push(this.nums)
      cal(dataArr, 3).then(response => {
        this.loading = false;
        this.title = "号码"
        this.numsOpen = true
        this.nums = response.msg;
        this.nsize = response.size;
        if (this.nums.includes(this.selectedItem[0].kjNum.substring(0, 3))) {
          this.zj = "中奖"
          this.tagType = "danger"
        } else {
          this.zj = "未中奖"
          this.tagType = "success"
        }
      });
    }
  }
};
</script>
